CREATE OR REPLACE FUNCTION lpd_get_prj_title_name(bigint,varchar) RETURNS varchar AS '
--version 0.1
--created by Postelnicu Mihai (mihai@code.ro)
DECLARE
       title_id ALIAS FOR $1;
       name_lang ALIAS FOR $2;
       cnt integer;
       avail integer;
       ret varchar;
       rec record;
       deflang varchar;
       availdefault integer;
BEGIN
     cnt:= COUNT(*) FROM gen_titles WHERE id=title_id;
     IF cnt = 0 THEN
     	RETURN ''This item has no name!'';
     END IF;
     avail:= COUNT(*) FROM gen_titles WHERE id=title_id AND lang=name_lang;
     IF avail = 1 THEN
    	SELECT INTO ret name FROM gen_titles WHERE id=title_id AND lang=name_lang;
    	RETURN ret;
     ELSE
        SELECT INTO deflang value FROM system_properties WHERE name=''defaultLang'';
        availdefault:= COUNT(*) FROM gen_titles WHERE id=title_id AND lang=deflang;
        IF availdefault = 1 THEN
	    	SELECT INTO ret name FROM gen_titles WHERE id=title_id AND lang=deflang;
	    	RETURN ret || '' ['' || deflang || '']'';
	ELSE
	       	FOR rec IN SELECT name, lang FROM gen_titles WHERE id=title_id
	       	LOOP
	       	    RETURN rec.name  || '' ['' || rec.lang || '']'';
	       	END LOOP;
	END IF;
     END IF;
END;
' language 'plpgsql';
